﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HCFramework.API.LINQ;
using HCFramework;

namespace HCFramework.Module.FeedBackModule.FrontEnd
{
    public partial class FeedBackInput : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitData();

                ButtonSend.Text = M.Common.GetLang(ButtonSend.Text);
            }
        }

        private void InitData()
        {
            if (C.UserController.IsLogin())
            {
                API.LINQ.User user = C.UserController.GetCurrentUser();
                TextBoxName.Text = user.FirstName + " " + user.LastName;
                TextBoxEmail.Text = user.Email;
            }
        }

        protected void ButtonSend_Click(object sender, EventArgs e)
        {
            // insert feedback
            API.LINQ.FeedBack f = new API.LINQ.FeedBack();
            f.Content = TextBoxContent.Text;
            f.Email = TextBoxEmail.Text;
            f.IPAddress = M.Common.GetIpAddress();
            f.Name = TextBoxName.Text;
            f.Subject = TextBoxSubjext.Text;
            f.Username = C.UserController.GetUsername();
            C.FeedBackController.Insert(f);

            // send email to admin
            string subject = M.FileM.Read(@"\FeedBack\Template\AdminEmailSubject.htm");
            string body = M.FileM.Read(@"\FeedBack\Template\AdminEmailBody.htm");
            subject = M.Template.MergeFeedBack(subject, f);
            body = M.Template.MergeFeedBack(body, f);
            M.Common.SendEmail(C.ConfigController.GetValue(E.Config.AdminEmailAddress), subject, body);

            // display message
            PanelMain.Visible = false;
            M.Message.ShowSuccess("Thông tin đã được gửi đi, chúng tôi sẽ trả lời bạn trong thời gian sớm nhất.");
        }
    }
}